package com.example.wendao.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import androidx.paging.PagedList;

import com.example.wendao.common.AsyncExecutor;
import com.example.wendao.common.PageUtils;
import com.example.wendao.model.entity.Forum;
import com.example.wendao.model.entity.ForumPics;
import com.example.wendao.model.entity.ForumTag;
import com.example.wendao.model.service.ForumPicsService;
import com.example.wendao.model.service.ForumService;
import com.example.wendao.model.service.ForumTagService;

public class ForumViewModel extends ViewModel {

    private ForumService forumService;

    private ForumTagService forumTagService;

    private ForumPicsService forumPicsService;

    /**
     *  当前论坛所有贴
     */
    private LiveData<PagedList<Forum>> allForum;

    {
        forumService = new ForumService();

        forumTagService = new ForumTagService();

        forumPicsService = new ForumPicsService();

        allForum = new PageUtils<Forum>().buildLivePagedList(forumService.findByAllForum());
    }

    public Forum[] findAllForumByForumAuthorIdInArray(int id){
        return  forumService.findAllForumByForumAuthorIdInArray(id);
    }

    /**
     * 返回全部的论坛帖
     * @return  全部的论坛帖
     */
    public LiveData<PagedList<Forum>> getAllForum(){
        return allForum;
    }

    /**
     * 根据传入的论坛ID序列查找到对应的论坛
     * @param forumIds  论坛ID序列 例如：（1,2,3,4,5）
     * @return          查询到的论坛分页列表
     */
    public LiveData<PagedList<Forum>> getForumsByForumIds(String forumIds){
        return new PageUtils<Forum>()
                .buildLivePagedList(forumService.findSomeForumsByForumIds(forumIds));
    }

    /**
     * 根据论坛ID查询到论坛帖
     * @param id    论坛ID
     * @return      查询到的论坛帖对象
     */
    public Forum getForumById(int id){
        return new AsyncExecutor<Forum>().submit(() -> forumService.findForumById(id));
    }

    /**
     * 增加一个论坛帖
     * @param forum 要添加的论坛帖对象
     */
    public void addForum(Forum forum){
        new AsyncExecutor<>().execute(() -> forumService.insertForum(forum));
    }


    /**
     * 按照用户id搜索其名下帖子
     * @param forumauthorid 要查询帖子的用户id
     * @return              查询到的帖子
     */
    public LiveData<PagedList<Forum>> getAllForumByForumAuthorId(int forumauthorid){
        return new PageUtils<Forum>().buildLivePagedList(forumService.findAllForumByForumAuthorId(forumauthorid));
    }

    public LiveData<PagedList<Forum>> getAllForumByTagId(int tagId){
        return new PageUtils<Forum>().buildLivePagedList(forumService.findAllForumByTagId(tagId));
    }


    /**
     * 按照用户id搜索其名下帖子，返回数组
     * @param forumauthorid 要查询帖子的用户id
     * @return              查询到的帖子
     */
    public Forum[] getAllForumByForumAuthorIdInArray(int forumauthorid){
        return new AsyncExecutor<Forum[]>().submit(() -> forumService.findAllForumByForumAuthorIdInArray(forumauthorid));
    }

    /**
     * 获取最新添加的一个论坛帖对象
     * @return 最新添加的一个论坛帖对象
     */
    public Forum getLatestForum(){
        return new AsyncExecutor<Forum>().submit(() -> forumService.findLatestForum());
    }

    /**
     * 按照标签id搜索帖子
     * @param forumtagid    论坛的标签ID
     * @return              搜索到的帖子列表
     */
    public LiveData<PagedList<Forum>> findAllForumByForumTagId(int forumtagid){
        return new PageUtils<Forum>().buildLivePagedList(forumService.findAllForumByForumTagId(forumtagid));
    }

    /**
     * 按照论坛帖标题搜索帖子
     * @param title 论坛帖标题
     * @return      搜索到的帖子列表
     */
    public LiveData<PagedList<Forum>> findForumByTitle(String title){
        return new PageUtils<Forum>().buildLivePagedList(forumService.findForumByTitle(title));
    }

    /**
     * 更新若干论坛贴
     * @param forum 要更新的帖子序列
     */
    public void updateForum(Forum... forum){
        new AsyncExecutor<>().execute(() -> forumService.updateForum(forum));
    }

    /**
     * 删除若干论坛贴
     * @param forums 要删除的帖子序列
     */
    public void deleteForum(Forum... forums){
        new AsyncExecutor<>().execute(() -> forumService.deleteForum(forums));
    }

    /**
     * 添加一个论坛标签
     * @param forumtag  要添加的标签
     */
    public void addTag(ForumTag forumtag) {
        new AsyncExecutor<>().execute(() -> forumTagService.addTag(forumtag));
    }

    /**
     * 根据论坛标签id查找到标签
     * @param tagid
     * @return
     */
    public ForumTag findForumTagByTagId(int tagid) {
        return new AsyncExecutor<ForumTag>().submit(() -> forumTagService.findForumTagByTagId(tagid));
    }

    /**
     * 根据标签名找到所有标签
     * @param tagname 标签名
     * @return 分页标签列表的数据源工厂
     */
    public LiveData<PagedList<ForumTag>> findForumTagByTagName(String tagname){
        return new PageUtils<ForumTag>().buildLivePagedList(forumTagService.findForumTagByTagName(tagname));
    }

    /**
     * 根据标签id查找到该类型标签的数量
     * @param tagid 标签id
     * @return      标签下帖子数量
     */
    public int findForumTagCountByTagId(int tagid) {
        return new AsyncExecutor<Integer>().submit(() -> forumTagService.findForumTagCountByTagId(tagid));
    }


    /**
     *
     * @param tagid 标签id
     */
    public void addForumTagCountByTagId(int tagid) {
        new AsyncExecutor<>().execute(() -> forumTagService.addForumTagCountByTagId(tagid));
    }


    /**
     *
     * @return 分页热门帖子列表的分页列表
     */
    public LiveData<PagedList<ForumTag>> findForumTagByHeat() {
        return new PageUtils<ForumTag>().buildLivePagedList(forumTagService.findForumTagByHeat());
    }


    /**
     * 添加一些图片
     * @param forumpics 要添加的图片
     */
    public void addForumPic(ForumPics... forumpics) {
        new AsyncExecutor<>().execute(() -> forumPicsService.insertForumPic(forumpics));
    }

    /**
     * 根据论坛id获取到其所属的图片
     * @param forumpicfatherid  要查找图片的论坛ID
     * @return  查找到的图片序列
     */
    public LiveData<PagedList<ForumPics>> findForumPicsByFatherId(int forumpicfatherid) {
        return new PageUtils<ForumPics>().buildLivePagedList(forumPicsService
                .findForumPicsByFatherId(forumpicfatherid));
    }


    /**
     * 根据论坛id查找封面
     * @param forumpicfatherid  论坛ID
     * @return                  查找到的封面图片
     */
    public ForumPics findForumPicsCoverByFatherId(int forumpicfatherid) {
        return new AsyncExecutor<ForumPics>().submit(() -> forumPicsService.findForumPicsCoverByFatherId(forumpicfatherid));
    }

    /**
     * 更新论坛图片
     * @param forumpics 要更新的论坛图片序列
     */
    public void updateForumPics(ForumPics... forumpics) {
        new AsyncExecutor<>().execute(() -> forumPicsService.updateForumPics(forumpics));
    }

    /**
     * 删除论坛图片
     * @param forumpics
     */
    public void deleteForumPics(ForumPics... forumpics) {
        new AsyncExecutor<>().execute(() -> forumPicsService.deleteForumPics(forumpics));
    }

}
